# Distributed under the OSI-approved BSD 3-Clause License.
#
#.rst:
# FindFFMPEG
# --------
#
# Find the FFPMEG libraries
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# The following variables will be defined:
#
#  ``FFMPEG_FOUND``
#    True if FFMPEG found on the local system
#
#  ``FFMPEG_INCLUDE_DIRS``
#    Location of FFMPEG header files
#
#  ``FFMPEG_LIBRARY_DIRS``
#    Location of FFMPEG libraries
#
#  ``FFMPEG_LIBRARIES``
#    List of the FFMPEG libraries found
#
#

include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
include(${CMAKE_ROOT}/Modules/SelectLibraryConfigurations.cmake)
include(${CMAKE_ROOT}/Modules/CMakeFindDependencyMacro.cmake)

set(FFMPEG_VERSION "4.2")

find_dependency(Threads)
if(UNIX)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS -pthread)
endif()
if(UNIX AND NOT APPLE)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS -lX11)
endif()

if(@ENABLE_BZIP2@)
  find_dependency(BZip2)
  get_target_property(BZip2_LIBRARY_RELEASE BZip2::BZip2 IMPORTED_LOCATION_RELEASE)
  get_target_property(BZip2_LIBRARY_DEBUG BZip2::BZip2 IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${BZip2_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${BZip2_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_ICONV@)
  find_dependency(unofficial-iconv)
  get_target_property(ICONV_LIBRARY_RELEASE unofficial::iconv::libiconv IMPORTED_LOCATION_RELEASE)
  get_target_property(ICONV_LIBRARY_DEBUG unofficial::iconv::libiconv IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${ICONV_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${ICONV_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_FDKAAC@)
  find_library(FDK_LIBRARY_RELEASE NAMES fdk-aac libfdk-aac PATHS ${_IMPORT_PREFIX}/lib/ NO_DEFAULT_PATH)
  find_library(FDK_LIBRARY_DEBUG NAMES fdk-aac libfdk-aac PATHS ${_IMPORT_PREFIX}/debug/lib/ NO_DEFAULT_PATH)
  select_library_configurations(FDK)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${FDK_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${FDK_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_LZMA@)
  find_dependency(LibLZMA)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${LibLZMA_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${LibLZMA_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_LAME@)
  find_dependency(mp3lame)
  get_target_property(LAME_LIBRARY_RELEASE mp3lame::mp3lame IMPORTED_LOCATION_RELEASE)
  get_target_property(LAME_LIBRARY_DEBUG mp3lame::mp3lame IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${LAME_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${LAME_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_OPENCL@)
  find_dependency(OpenCL)
  get_target_property(OPENCL_LIBRARY_RELEASE OpenCL::OpenCL IMPORTED_LOCATION_RELEASE)
  get_target_property(OPENCL_LIBRARY_DEBUG OpenCL::OpenCL IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${OPENCL_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${OPENCL_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_OPENSSL@)
  find_dependency(OpenSSL)
  get_target_property(LIBSSL_LIBRARY_RELEASE OpenSSL::SSL IMPORTED_LOCATION_RELEASE)
  get_target_property(LIBSSL_LIBRARY_DEBUG OpenSSL::SSL IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${LIBSSL_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${LIBSSL_LIBRARY_RELEASE}>")
  get_target_property(LIBCRYPTO_LIBRARY_RELEASE OpenSSL::Crypto IMPORTED_LOCATION_RELEASE)
  get_target_property(LIBCRYPTO_LIBRARY_DEBUG OpenSSL::Crypto IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${LIBCRYPTO_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${LIBCRYPTO_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_OPUS@)
  find_dependency(Opus)
  get_target_property(OPUS_LIBRARY_RELEASE Opus::opus IMPORTED_LOCATION_RELEASE)
  get_target_property(OPUS_LIBRARY_DEBUG Opus::opus IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${OPUS_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${OPUS_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_SDL2@)
  find_dependency(SDL2)
  get_target_property(SDL2_LIBRARY_RELEASE SDL2::SDL2 IMPORTED_LOCATION_RELEASE)
  get_target_property(SDL2_LIBRARY_DEBUG SDL2::SDL2 IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${SDL2_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${SDL2_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_SNAPPY@)
  find_dependency(Snappy)
  get_target_property(SNAPPY_LIBRARY_RELEASE Snappy::snappy IMPORTED_LOCATION_RELEASE)
  get_target_property(SNAPPY_LIBRARY_DEBUG Snappy::snappy IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${SNAPPY_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${SNAPPY_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_SOXR@)
  find_library(SOXR_LIBRARY_RELEASE NAMES soxr PATHS ${_IMPORT_PREFIX}/lib/ NO_DEFAULT_PATH)
  find_library(SOXR_LIBRARY_DEBUG NAMES soxr PATHS ${_IMPORT_PREFIX}/debug/lib/ NO_DEFAULT_PATH)
  select_library_configurations(SOXR)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${SOXR_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${SOXR_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_SPEEX@)
  find_library(SPEEX_LIBRARY_RELEASE NAMES speex libspeex PATHS ${_IMPORT_PREFIX}/lib/ NO_DEFAULT_PATH)
  find_library(SPEEX_LIBRARY_DEBUG NAMES speex libspeex PATHS ${_IMPORT_PREFIX}/debug/lib/ NO_DEFAULT_PATH)
  select_library_configurations(SPEEX)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${SPEEX_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${SPEEX_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_THEORA@)
  find_library(THEORA_LIBRARY_RELEASE NAMES theora PATHS ${_IMPORT_PREFIX}/lib/ NO_DEFAULT_PATH)
  find_library(THEORA_LIBRARY_DEBUG NAMES theora PATHS ${_IMPORT_PREFIX}/debug/lib/ NO_DEFAULT_PATH)
  select_library_configurations(SPEEX)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${THEORA_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${THEORA_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_VORBIS@)
  find_dependency(Vorbis)
  get_target_property(VORBIS_LIBRARY_RELEASE Vorbis::vorbis IMPORTED_LOCATION_RELEASE)
  get_target_property(VORBIS_LIBRARY_DEBUG Vorbis::vorbis IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${VORBIS_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${VORBIS_LIBRARY_RELEASE}>")
  get_target_property(VORBISENC_LIBRARY_RELEASE Vorbis::vorbisenc IMPORTED_LOCATION_RELEASE)
  get_target_property(VORBISENC_LIBRARY_DEBUG Vorbis::vorbisenc IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${VORBISENC_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${VORBISENC_LIBRARY_RELEASE}>")
  get_target_property(VORBIS_DEP_LIBRARIES Vorbis::vorbis INTERFACE_LINK_LIBRARIES)
  foreach(deps ${VORBIS_DEP_LIBRARIES})
    if(TARGET ${deps})
      get_target_property(VORBIS_DEP_LIBRARY_RELEASE ${deps} IMPORTED_LOCATION_RELEASE)
      get_target_property(VORBIS_DEP_LIBRARY_DEBUG ${deps} INTERFACE_LINK_LIBRARIES)
      list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${VORBIS_DEP_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${VORBIS_DEP_LIBRARY_RELEASE}>")
    endif()
  endforeach()
endif()

if(@ENABLE_VPX@)
  find_dependency(unofficial-libvpx)
  get_target_property(VPX_LIBRARY_RELEASE unofficial::libvpx::libvpx IMPORTED_LOCATION_RELEASE)
  get_target_property(VPX_LIBRARY_DEBUG unofficial::libvpx::libvpx IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${VPX_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${VPX_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_WAVPACK@)
  find_dependency(wavpack)
  get_target_property(WAVPACK_LIBRARY_RELEASE WavPack::wavpack IMPORTED_LOCATION_RELEASE)
  get_target_property(WAVPACK_LIBRARY_DEBUG WavPack::wavpack IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${WAVPACK_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${WAVPACK_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_X264@)
  find_library(X264_LIBRARY_RELEASE NAMES x264 libx264 PATHS ${_IMPORT_PREFIX}/lib/ NO_DEFAULT_PATH)
  find_library(X264_LIBRARY_DEBUG NAMES x264 libx264 PATHS ${_IMPORT_PREFIX}/debug/lib/ NO_DEFAULT_PATH)
  select_library_configurations(X264)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${X264_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${X264_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_X265@)
  find_library(X265_LIBRARY_RELEASE NAMES x265 x265-static PATHS ${_IMPORT_PREFIX}/lib/ NO_DEFAULT_PATH)
  find_library(X265_LIBRARY_DEBUG NAMES x265 x265-static PATHS ${_IMPORT_PREFIX}/debug/lib/ NO_DEFAULT_PATH)
  select_library_configurations(X265)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${X265_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${X265_LIBRARY_RELEASE}>")
endif()

if(@ENABLE_ZLIB@)
  find_dependency(ZLIB)
  get_target_property(ZLIB_LIBRARY_RELEASE ZLIB::ZLIB IMPORTED_LOCATION_RELEASE)
  get_target_property(ZLIB_LIBRARY_DEBUG ZLIB::ZLIB IMPORTED_LOCATION_DEBUG)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS "$<$<CONFIG:Debug>:${ZLIB_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${ZLIB_LIBRARY_RELEASE}>")
endif()

#  Platform dependent libraries required by FFMPEG
if(WIN32)
  if(NOT CYGWIN)
    list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS wsock32 ws2_32 secur32 bcrypt strmiids Vfw32 Shlwapi)
  endif()
else()
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS m)
endif()

# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
  set(_IMPORT_PREFIX "")
endif()

set(_IMPORT_PREFIX)

macro(FFMPEG_FIND varname shortname headername)
  if(NOT FFMPEG_${varname}_INCLUDE_DIRS)
    find_path(FFMPEG_${varname}_INCLUDE_DIRS NAMES lib${shortname}/${headername} ${headername} PATHS ${_IMPORT_PREFIX}/include NO_DEFAULT_PATH)
  endif()
  if(NOT FFMPEG_${varname}_LIBRARY)
    find_library(FFMPEG_${varname}_LIBRARY_RELEASE NAMES ${shortname} PATHS ${_IMPORT_PREFIX}/lib/ NO_DEFAULT_PATH)
    find_library(FFMPEG_${varname}_LIBRARY_DEBUG NAMES ${shortname}d ${shortname} PATHS ${_IMPORT_PREFIX}/debug/lib/ NO_DEFAULT_PATH)
    get_filename_component(FFMPEG_${varname}_LIBRARY_RELEASE_DIR ${FFMPEG_${varname}_LIBRARY_RELEASE} DIRECTORY)
    get_filename_component(FFMPEG_${varname}_LIBRARY_DEBUG_DIR ${FFMPEG_${varname}_LIBRARY_DEBUG} DIRECTORY)
    #select_library_configurations(FFMPEG_${varname})
    set(FFMPEG_${varname}_LIBRARY "$<$<CONFIG:Debug>:${FFMPEG_${varname}_LIBRARY_DEBUG}>$<$<CONFIG:Release>:${FFMPEG_${varname}_LIBRARY_RELEASE}>" CACHE STRING "")
    set(FFMPEG_${varname}_LIBRARIES ${FFMPEG_${varname}_LIBRARY} CACHE STRING "")
  endif()
  if (FFMPEG_${varname}_LIBRARY AND FFMPEG_${varname}_INCLUDE_DIRS)
    set(FFMPEG_${varname}_FOUND TRUE BOOL)
    list(APPEND FFMPEG_INCLUDE_DIRS ${FFMPEG_${varname}_INCLUDE_DIRS})
    list(APPEND FFMPEG_LIBRARIES ${FFMPEG_${varname}_LIBRARY})
    list(APPEND FFMPEG_LIBRARY_DIRS ${FFMPEG_${varname}_LIBRARY_RELEASE_DIR} ${FFMPEG_${varname}_LIBRARY_DEBUG_DIR})
  endif()
endmacro(FFMPEG_FIND)

if(APPLE)
  find_library(VT_UNIT VideoToolbox)
  if (NOT VT_UNIT)
      message(FATAL_ERROR "VideoToolbox not found")
  endif()
  find_library(AT_UNIT AudioToolbox)
  if (NOT AT_UNIT)
      message(FATAL_ERROR "AudioToolbox not found")
  endif()
  find_library(SEC_UNIT Security)
  if (NOT SEC_UNIT)
      message(FATAL_ERROR "Security not found")
  endif()
  find_library(CF_UNIT CoreFoundation)
  if (NOT CF_UNIT)
      message(FATAL_ERROR "CoreFoundation not found")
  endif()
  find_library(CM_UNIT CoreMedia)
  if (NOT CM_UNIT)
      message(FATAL_ERROR "CoreMedia not found")
  endif()
  find_library(CV_UNIT CoreVideo)
  if (NOT CV_UNIT)
      message(FATAL_ERROR "CoreVideo not found")
  endif()
  find_package(Iconv QUIET)
  list(APPEND FFMPEG_PLATFORM_DEPENDENT_LIBS ${VT_UNIT} ${AT_UNIT} ${SEC_UNIT} ${CF_UNIT} ${CM_UNIT} ${CV_UNIT} ${Iconv_LIBRARIES})
endif()

if(@ENABLE_AVCODEC@)
  FFMPEG_FIND(libavcodec    avcodec    avcodec.h)
endif()
if(@ENABLE_AVDEVICE@)
  FFMPEG_FIND(libavdevice   avdevice   avdevice.h)
endif()
if(@ENABLE_AVFILTER@)
  FFMPEG_FIND(libavfilter   avfilter   avfilter.h)
endif()
if(@ENABLE_AVFORMAT@)
  FFMPEG_FIND(libavformat   avformat   avformat.h)
endif()
if(@ENABLE_AVRESAMPLE@)
  FFMPEG_FIND(libavresample avresample avresample.h)
endif()
FFMPEG_FIND(libavutil       avutil     avutil.h)
if(@ENABLE_POSTPROC@)
  FFMPEG_FIND(libpostproc   postproc   postprocess.h)
endif()
if(@ENABLE_SWRESAMPLE@)
  FFMPEG_FIND(libswresample swresample swresample.h)
endif()
if(@ENABLE_SWSCALE@)
  FFMPEG_FIND(libswscale    swscale    swscale.h)
endif()

if (FFMPEG_libavutil_FOUND)
  list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
  list(REMOVE_DUPLICATES FFMPEG_LIBRARY_DIRS)
  set(FFMPEG_libavutil_VERSION "${FFMPEG_VERSION}" CACHE STRING "")

  if(FFMPEG_libavcodec_FOUND)
    set(FFMPEG_libavcodec_VERSION "${FFMPEG_VERSION}" CACHE STRING "")
  endif()
  if(FFMPEG_libavdevice_FOUND)
    set(FFMPEG_libavdevice_VERSION "${FFMPEG_VERSION}" CACHE STRING "")
  endif()
  if(FFMPEG_libavfilter_FOUND)
    set(FFMPEG_libavfilter_VERSION "${FFMPEG_VERSION}" CACHE STRING "")
  endif()
  if(FFMPEG_libavformat_FOUND)
    set(FFMPEG_libavformat_VERSION "${FFMPEG_VERSION}" CACHE STRING "")
  endif()
  if(FFMPEG_libavresample_FOUND)
    set(FFMPEG_libavresample_VERSION "${FFMPEG_VERSION}" CACHE STRING "")
  endif()
  if(FFMPEG_libswresample_FOUND)
    set(FFMPEG_libswresample_VERSION "${FFMPEG_VERSION}" CACHE STRING "")
  endif()
  if(FFMPEG_libswscale_FOUND)
    set(FFMPEG_libswscale_VERSION "${FFMPEG_VERSION}" CACHE STRING "")
  endif()

  list(APPEND FFMPEG_LIBRARIES
    ${FFMPEG_PLATFORM_DEPENDENT_LIBS}
  )

  set(FFMPEG_LIBRARY ${FFMPEG_LIBRARIES})

  set(FFMPEG_FOUND TRUE CACHE BOOL "")
  set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "")
  set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "")
  set(FFMPEG_LIBRARY_DIRS ${FFMPEG_LIBRARY_DIRS} CACHE STRING "")
endif()

find_package_handle_standard_args(FFMPEG REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_LIBRARY_DIRS FFMPEG_INCLUDE_DIRS)
